package com.example;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import static java.lang.Math.min;

import static java.lang.Math.max;
/**
 * Created by gerrie on 12/04/2019.
 */
public class AlibabaApril {
    private static class Rect {
        private int left;
        private int top;
        private int width;
        private int height;
    }
    public static Integer test[] = {5,
            232,
            66,
            111,
            41,
            197,
            44,
            29,
            53,
            154,
            208,
            42,
            12,
            177,
            87,
            9,
            102,
            75,
            168,
            79,
            41};
    public static void test( ) {
//        Scanner in = new Scanner(System.in);
        List<Integer> data =  Arrays.asList(test);
        Iterator<Integer> iter = data.iterator();
        int n = iter.next();
        Rect[] rects = new Rect[n];
        for (int i = 0; i < n; i++) {
            rects[i] = new Rect();
            rects[i].left = iter.next();
            rects[i].top = iter.next();
            rects[i].width = iter.next();
            rects[i].height = iter.next();
        }
        System.out.println(String.valueOf(costTime(rects)));
    }

    private static long renderCost(Rect r){
        return 10000 + r.width * r.height;
    }

    private static Rect isWorthCombine(Rect r1, Rect r2){
        long origin = renderCost(r1) + renderCost(r2);
        Rect newRect = new Rect();
        int l = min(r1.left, r2.left);
        int t = min(r1.top, r2.top);
        int r = Math.max(r1.left + r1.width, r2.left + r2.width);
        int b = max(r1.top + r1.height, r2.top + r2.height);
        newRect.left = l;
        newRect.top = t;
        newRect.width = r - l;
        newRect.height = b - t;
        int newCost = (int) renderCost(newRect);
        if (newCost < origin)
            return newRect;
        else
            return null;
        //todo 比较面积即可
    }

    private static List<Rect> sortByArea(Rect [] rect){
        System.out.println(rect);
        Arrays.sort(rect, new Comparator<Rect>() {
            @Override
            public int compare(Rect r1, Rect r2) {
                if(r1.height * r1.width < r2.height* r2.width)
                    return 1;
                else
                    return -1;
            }
        });
        return null;
    }
    private static long area(Rect r){
        return r.width * r.height;
    }

    private static long costTime(Rect[] rects) {
        //TODO
        sortByArea(rects);
        int sumCost = 0;
        boolean []renderMark = new boolean[rects.length];
        for (int i = 0; i < rects.length; i++) {
            if(renderMark[i])
                continue;
            Rect ri = rects[i];
            for (int j = i + 1 ; j < rects.length; j++) {
                if(renderMark[j])
                    continue;
                Rect rj = rects[j];
                Rect newRect = isWorthCombine(rj, ri);
                if(newRect !=null){
//                    sumCost += area(newRect);
                    rects[j] = newRect;
                    renderMark[i] = true;
                }


            }
            if(!renderMark[i])
                sumCost += renderCost(ri);
            System.out.println("area" + ri.width * ri.height);
//45291
        }
        return sumCost;
    }

}
